package org.lex.ftp.ui.explorer;

import java.awt.Component;
import javax.swing.*;

import org.lex.ftp.ui.explorer.actions.*;

public class TablePopupMenu extends JPopupMenu {
	private FTPExplorer explorer = null;
	// ==============================
	private Action transfer = null;
	private Action queue = null;
	private Action makeDir = null;
	private Action rename = null;
	private Action move = null;
	private Action delete = null;
	private Action copyURL = null;
	private Action refresh = null;
	// ===============================
	private Action[] zeroSelection = null;
	private Action[] singleSelection = null;
	private Action[] multiSelection = null;
	private Action[] allActions = null;

	public TablePopupMenu(FTPExplorer explorer) {
		super();
		this.explorer = explorer;
		// initiaize actions
		this.createActions();
		// add actions to popup menu
		this.add(transfer);
		this.add(queue);
		this.addSeparator();
		this.add(copyURL);
		this.add(makeDir);
		this.add(rename);
		this.add(move);
		this.add(delete);
		this.addSeparator();
		this.add(refresh);
		// config table input/action map
		JTable table = explorer.getFileExplorer();
		InputMap im = table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
		ActionMap am = table.getActionMap();
		for (int i = 0; i < this.allActions.length; i++) {
			Action a = allActions[i];
			KeyStroke ks = (KeyStroke) a.getValue(Action.ACCELERATOR_KEY);
			if (null == ks)
				continue;
			else {
				String name = (String) a.getValue(Action.NAME);
				am.put(name, a);
				im.put(ks, a);
			}
		}
	}

	private void createActions() {
		transfer = new TransferAction(explorer);
		queue = new QueueAction(explorer);
		makeDir = new MakeFolderAction(explorer);
		rename = new RenameAction(explorer);
		move = new MoveAction(explorer);
		delete = new DeleteAction(explorer);
		copyURL = new CopyURLAction(explorer);
		refresh = new RefreshAction(explorer);
		// =======================================
		this.zeroSelection = new Action[] { transfer, makeDir, refresh };
		this.singleSelection = new Action[] { transfer, queue, makeDir, rename, move, delete, copyURL, refresh };
		this.multiSelection = new Action[] { transfer, queue, makeDir, move, delete, copyURL, refresh };
		this.allActions = new Action[] { transfer, queue, makeDir, rename, move, delete, copyURL, refresh };
	}

	public void show(Component invoker, int x, int y) {
		int count = explorer.getFileExplorer().getSelectedFilesCount();
		if (0 == count)
			this.setupActions(this.zeroSelection);
		else if (1 == count)
			this.setupActions(this.singleSelection);
		else
			this.setupActions(this.multiSelection);
		super.show(invoker, x, y);
	}

	private void setupActions(Action[] actions) {
		for (int i = 0; i < allActions.length; i++) {
			Action a = allActions[i];
			if (true == this.hasReference(actions, a))
				a.setEnabled(true);
			else
				a.setEnabled(false);
		}
	}

	private boolean hasReference(Action[] array, Action a) {
		for (int i = 0; i < array.length; i++)
			if (a == array[i])
				return true;
		return false;
	}
}
